package com.core.reach.cloudhub.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.core.reach.cloudhub.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ga.zhang
 * @version V1.0
 * @description: TODO
 * @date 2020/3/30 20:02
 **/
@Slf4j
@RestController
public class FlowLimitController {

    @Resource
    private OrderService orderService;

    @GetMapping("/test/A")
    public String testA(){
        orderService.getOrder();
        return "test____A";
    }

    @GetMapping("/test/B")
    public String testB(){
        log.info("当前线程：{}，结果是：{}",Thread.currentThread().getName(),"test____B");
        return "test____B";
    }

    @GetMapping("/test/C")
    public String testC(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("testC 测试RT");
        return "testC";
    }

    @GetMapping("/test/D")
    public String testD(){
        int i = 10/0;
        log.info("testC 测试异常比例");
        return "testD";
    }

    @GetMapping("/test/E")
    public String testE(){
        int i = 10/0;
        log.info("testC 测试异常数");
        return "testE";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "dealTestHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2){
        log.info("testHotKey 测试HotKey");
        return "testHotKey";
    }

    /**
     * 兜底方法
     * @param p1 参数
     * @param p2 参数
     * @param be 异常
     * @return String
     */
    public String dealTestHotKey(String p1, String p2, BlockException be){
        return "dealTestHotKey，(╥╯^╰╥)";
    }
}
